package cn.cdeden.system.controller.system;

import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.core.validate.AddGroup;
import cn.cdeden.common.core.validate.EditGroup;
import cn.cdeden.common.excel.utils.ExcelUtil;
import cn.cdeden.common.idempotent.annotation.RepeatSubmit;
import cn.cdeden.common.log.annotation.Log;
import cn.cdeden.common.log.enums.BusinessType;
import cn.cdeden.common.mybatis.core.page.PageQuery;
import cn.cdeden.common.web.core.BaseController;
import cn.cdeden.system.domain.bo.SysLargeModelBo;
import cn.cdeden.system.domain.vo.SysLargeModelVo;
import cn.cdeden.system.service.ISysLargeModelService;
import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 大模型相关配置
 * Ollama ip地址
 * 连接的模型
 *
 *
 */
@RestController
@RequestMapping("/large/model/config")
@RequiredArgsConstructor
public class LargeModelController extends BaseController {

    private final ISysLargeModelService sysLargeModelService;


    /**
     * 查询大模型配置列表
     */
    @SaCheckPermission("system:largeModel:list")
    @GetMapping("/list")
    public TableDataInfo<SysLargeModelVo> list(SysLargeModelBo bo, PageQuery pageQuery) {
        return sysLargeModelService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出登录方式配置列表
     */
    @SaCheckPermission("system:largeModel:export")
    @Log(title = "登录方式配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysLargeModelBo bo, HttpServletResponse response) {
        List<SysLargeModelVo> list = sysLargeModelService.queryList(bo);
        ExcelUtil.exportExcel(list, "登录方式配置", SysLargeModelVo.class, response);
    }




    /**
     * 获取大模型配置详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:largeModel:query")
    @GetMapping("/{id}")
    public R<SysLargeModelVo> getInfo(@NotNull(message = "主键不能为空")
                                        @PathVariable Long id) {
        return R.ok(sysLargeModelService.queryById(id));
    }


    /**
     * 根据参数键名查询参数值
     *
     * @param configKey 参数Key
     */
    @GetMapping(value = "/configKey/getValueByKey")
    public R<String> getConfigKey(@RequestParam String configKey) {
        return R.ok("操作成功", sysLargeModelService.selectConfigByKey(configKey));
    }


    /**
     * 新增大模型配置
     */
    @SaCheckPermission("system:largeModel:add")
    @Log(title = "大模型配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysLargeModelBo bo) {
        if (!sysLargeModelService.checkConfigKeyUnique(bo)) {
            return R.fail("新增参数'" + bo.getConfigName() + "'失败，参数键名已存在");
        }
        return toAjax(sysLargeModelService.insertByBo(bo));
    }


    /**
     * 修改大模型配置
     */
    @SaCheckPermission("system:largeModel:edit")
    @Log(title = "大模型配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<String> edit(@Validated(EditGroup.class) @RequestBody SysLargeModelBo bo) {
        if (!sysLargeModelService.checkConfigKeyUnique(bo)) {
            return R.fail("修改参数'" + bo.getConfigName() + "'失败，参数键名已存在");
        }
        if (!sysLargeModelService.checkConfigDefault(bo)) {
            return R.fail("修改参数'" + bo.getDefaultOptions() + "' 失败，已经有默认大模型了");
        }
        return R.ok(sysLargeModelService.updateByBo(bo));
    }


    /**
     * 根据参数键名修改参数配置
     */
    @SaCheckPermission("system:largeModel:edit")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping("/updateByKey")
    public R<Void> updateByKey(@RequestBody SysLargeModelBo config) {
        sysLargeModelService.updateConfig(config);
        return R.ok();
    }


    /**
     * 删除大模型配置
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:largeModel:remove")
    @Log(title = "登录方式配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysLargeModelService.deleteWithValidByIds(List.of(ids), true));
    }


    /**
     * 刷新参数缓存
     */
    @SaCheckPermission("system:config:remove")
    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public R<Void> refreshCache() {
        sysLargeModelService.resetConfigCache();
        return R.ok();
    }





}
